*****************************************************************************
* IMFTAX paper - SCID replication
*****************************************************************************


* Data preparation
* ***************************************************************

  cd "E:\_imf\IMFTAX\Submissions\SCID-R2"
  use "SCID_replication", clear
    
  drop if region_WDI=="High-Income Economies"
    
  g lngdppc=ln(gdppc_WDI)
  g lnpop=ln(pop_tot_WDI)
  g lnpopden=ln(pop_density)
  g depratio=pop_0014+pop_65nn
  g lnoilpc=ln(1+ross_oil_prod/unna_pop)
  g lnodapc=ln(1+oda_pc)
  g fhindex=14-fh_pr-fh_cl
  g ssa=(regid==7)
  g left=(dpi_erlc==3 |dpi_gprlc==3)
  
  g IMFcum=.
  forvalues i=1980/2014{
    g imf=IMFnn
	replace imf=. if year>`i' |year<`i'-5
	egen IMF=sum(imf), by(cid)
	replace IMFcum=IMF if year==`i'
	drop imf IMF
  }
  lab var IMFcum "Past IMF years (t-5<=x<=t-1)"

  cmp setup
  set matsize 800
  qui xi:reg tottax i.cid i.year i.regid
  sort cid year
  xtset cid year  
     
  egen sp_taxany1=sum(taxany1), by(year regid)
  replace sp_taxany1=sp_taxany1-taxany1
   
  egen sp_totrev=mean(totrev), by(year regid)
  egen n=count(totrev), by(year regid)
  replace sp_totrev=sp_totrev- totrev/n
  corr sp_taxany1 sp_totrev taxany1 totrev
  sort cid year 
  
  

* Illustrative evidence  
* ***************************************************************
  
* Tax revenue  
  
  preserve   
  
  egen imf=sum(IMFnn), by(cid)
  replace imf=(imf>0)
  egen nimf=sum(imf), by(year)
  egen nall=count(imf), by(year)
  collapse tottax domtax trade gs income corp nimf nall, by(year imf)
  egen all=rowtotal(trade gs income corp)
  g ptrade=100*trade/all
  g pgs=100*gs/all
  g pinc=100*income/all
  g pcor=100*corp/all
  replace pgs=. if imf==0 & year==2013
  replace ptrade=. if imf==0 & year==2013 
  
  ** Figure 1 **
  twoway connected p* year if year<=2013, by(imf) scheme(s1mono) ytitle(Tax revenue (% GDP)) xtitle()

  restore 

  
* Tax types   

  preserve

  collapse (sum)taxany1 taxgst1 taxtr1 taxcrp1 IMFnn, by(year)
  g taxanyimf=100*taxany1/IMFnn
  g taxgstimf=100*taxgst1/IMFnn
  g taxtrimf=100*taxtr1/IMFnn
  g taxcrpimf=100*taxcrp1/IMFnn
  
  ** Figure 2 **
  twoway connected taxanyimf taxgstimf taxtrimf year if year<=2013, scheme(s1mono) ytitle(Percentage of programs with tax condition) xtitle()
  
  restore 
 
  
* Tax adoption

  preserve

  egen imf=sum(IMFnn), by(cid)
  replace imf=(imf>0)
  collapse (sum)VAT GST CIT PIT, by(year imf)
  
  ** Figure A1 **
  twoway (connected VAT PIT CIT year) if year<2014, by(imf)
  
  restore

  
* Cases

  twoway (connected trade gs year) if iso3=="RWA" 
  twoway (connected trade gs income corp year) if iso3=="RWA" 

  
  
* Main analysis
* ***************************************************************  
  
  global L1 l.lngdppc l.totnontax l.trade_WDI l.va_agr_gdp_WDI l.gdp_growth_WDI
  global ZL l2.IMFcum l2.nUnder l2.s_unga3g7 l2.lngdppc l2.gdp_growth l2.reserves l2.extbal l2.debtser_gni l2.fhindex l2.dpi_exelec _Iregid*
  g CGyears=(year>=1993 & year<=2013)


* Descriptives 

  corr gs trade income corp IMFnn taxany1 taxgst1 taxtr1 if CGyears
  estpost summarize tottax gs trade income corp IMFnn taxany1 taxgst1 taxtr1 lngdppc totnontax trade_W va_agr gdp_growth IMFcum nUnder s_unga3 reserves extbal debtser_gni fhindex dpi_exelec imf_co imf_ro if CGyears

  ** Table 1 **
  esttab, cells("count mean sd min max") noobs replace label  
  
  
**************************  
* SUR system for IMF dummy
**************************

  mat r2=J(3,12,0)
  
  forvalues l=1/3{
  
  local k=`l'+1
  qui g LagIMFnn=l`l'.IMFnn
  di "Lag `l'"
  
  foreach var in lngdppc totnontax trade_WDI va_agr_gdp_WDI gdp_growth_WDI{
    qui g Lag`var'=l`l'.`var'
	}
  foreach var in IMFcum nUnder s_unga3g7 lngdppc gdp_growth reserves extbal debtser_gni fhindex dpi_exelec{
    qui g Lagz`var'=l`k'.`var'
	}
  
  global L1 Laglngdppc Lagtotnontax Lagtrade_WDI Lagva_agr_gdp_WDI Laggdp_growth_WDI
  global ZL LagzIMFcum LagznUnder Lagzs_unga3g7 Lagzlngdppc Lagzgdp_growth Lagzreserves Lagzextbal Lagzdebtser_gni Lagzfhindex Lagzdpi_exelec _Iregid*
  
  qui cmp (gs=l.gs LagIMFnn $L1 _Icid* _Iyear*) (trade=l.trade LagIMFnn $L1 _Icid* _Iyear*) (corp=l.corp LagIMFnn $L1 _Icid* _Iyear*) (income=l.income LagIMFnn $L1 _Icid* _Iyear*) (LagIMFnn=$ZL _Iyear*) if CGyears, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont $cmp_probit) ro cl(cid) iterate(200)
  est store e1`l'
  g e1sample=e(sample)
  
  qui probit LagIMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  local m=0
  foreach y in gs trade corp income{
	qui xtreg `y' l.`y' LagIMFnn $L1 _Iyear* if CGyears & e1sample, fe
	mat r2[1,3*`m'+`l']=e(N)
	mat r2[2,3*`m'+`l']=e(r2_w)
	local m=`m'+1
	}
  drop Lag* e1sample
  }


  ** Table 2 **
  estout e1* using "SCID_Table 2.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) drop(_I* _cons) keep() order(*IMF* $L1) unstack 
  mat li r2    
  
  
* SUR restrictions

  mat sur=J(6,6,0)
  mat surplus=sur
  
  forvalues l=1/3{
    est restore e1`l'
    mat sur[2*`l'-1,1]=([gs]LagIMFnn-[trade]LagIMFnn)
    qui test ([gs]LagIMFnn=[trade]LagIMFnn)
    mat sur[2*`l',1]=r(p)
	mat surplus[2*`l'-1,1]=([gs]LagIMFnn+[trade]LagIMFnn)
    qui test ([gs]LagIMFnn=-[trade]LagIMFnn)
    mat surplus[2*`l',1]=r(p)/2

    mat sur[2*`l'-1,2]=([gs]LagIMFnn-[corp]LagIMFnn)
    qui test ([gs]LagIMFnn=[corp]LagIMFnn)
    mat sur[2*`l',2]=r(p)
	mat surplus[2*`l'-1,2]=([gs]LagIMFnn+[corp]LagIMFnn)
    qui test ([gs]LagIMFnn=-[corp]LagIMFnn)
    mat surplus[2*`l',2]=r(p)/2

    mat sur[2*`l'-1,3]=([gs]LagIMFnn-[income]LagIMFnn)
    qui test ([gs]LagIMFnn=[income]LagIMFnn)
    mat sur[2*`l',3]=r(p)
	mat surplus[2*`l'-1,3]=([gs]LagIMFnn+[income]LagIMFnn)
    qui test ([gs]LagIMFnn=-[income]LagIMFnn)
    mat surplus[2*`l',3]=r(p)/2

    mat sur[2*`l'-1,4]=([trade]LagIMFnn-[corp]LagIMFnn)
    qui test ([trade]LagIMFnn=[corp]LagIMFnn)
    mat sur[2*`l',4]=r(p)
	mat surplus[2*`l'-1,4]=([trade]LagIMFnn+[corp]LagIMFnn)
    qui test ([trade]LagIMFnn=-[corp]LagIMFnn)
    mat surplus[2*`l',4]=r(p)/2

    mat sur[2*`l'-1,5]=([trade]LagIMFnn-[income]LagIMFnn)
    qui test ([trade]LagIMFnn=[income]LagIMFnn)
    mat sur[2*`l',5]=r(p)
	mat surplus[2*`l'-1,5]=([trade]LagIMFnn+[income]LagIMFnn)
    qui test ([trade]LagIMFnn=-[income]LagIMFnn)
    mat surplus[2*`l',5]=r(p)/2

    mat sur[2*`l'-1,6]=([corp]LagIMFnn-[income]LagIMFnn)
    qui test ([corp]LagIMFnn=[income]LagIMFnn)
    mat sur[2*`l',6]=r(p)
	mat surplus[2*`l'-1,6]=([corp]LagIMFnn+[income]LagIMFnn)
    qui test ([corp]LagIMFnn=-[income]LagIMFnn)
    mat surplus[2*`l',6]=r(p)/2
  }

  ** Table B1 **
  mat li sur
  
  ** Table B2 **
  mat li surplus
  
  
********************
* Tax conditionality 
********************

* Durbin-Wu-Hausman test of endogeneity

  qui probit l.taxany1 l.IMFnn l.imf_co l.imf_ro l.lngdppc l.totnontax l.trade_WDI l.va_agr_gdp_WDI l.gdp_growth_WDI if CGyears
  predict linpred, xb
  g imrtax=normalden(linpred)/normal(linpred)

  qui cmp (gs=l.gs l.taxany1 imrtax l.IMFnn l.lngdppc l.totnontax l.trade_WDI l.va_agr_gdp_WDI l.gdp_growth_WDI _Icid* _Iyear*) (l.IMFnn=l2.IMFcum l2.nUnder l2.s_unga3g7 l2.lngdppc l2.gdp_growth l2.reserves l2.extbal l2.debtser_gni l2.fhindex l2.dpi_exelec _Iregid* _Iyear*) if CGyears, indicators($cmp_cont $cmp_probit) ro cl(cid) iterate(50)
  test ([gs]imrtax)

* Analysis

  mat tc=J(4,12,0)
  mat r2=J(4,12,0)

  forvalues l=1/3{
  
  local k=`l'+1
  di "Lag `l'"
  
  foreach var in lngdppc totnontax trade_WDI va_agr_gdp_WDI gdp_growth_WDI{
    qui g L`l'`var'=l`l'.`var'
	}
  foreach var in IMFcum nUnder s_unga3g7 lngdppc gdp_growth reserves extbal debtser_gni fhindex dpi_exelec{
    qui g L`k'`var'=l`k'.`var'
	}
  foreach var in IMFnn taxany1 taxgst1 taxtr1 taxcrp1 imf_co imf_ro{
    qui g L`l'`var'=l`l'.`var'
	}
  
  global L1 L`l'lngdppc L`l'totnontax L`l'trade_WDI L`l'va_agr_gdp_WDI L`l'gdp_growth_WDI
  global ZL L`k'IMFcum L`k'nUnder L`k's_unga3g7 L`k'lngdppc L`k'gdp_growth L`k'reserves L`k'extbal L`k'debtser_gni L`k'fhindex L`k'dpi_exelec _Iregid*
  global TL L`l'IMFnn L`l'imf_co L`l'imf_ro
  
  ** Estimation ** 
  qui cmp (gs=l.gs L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (trade=l.trade L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (corp=l.corp L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (income=l.income L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxany1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(200)
  est store e2`l'
  mat B=e(b)
  mat S=e(V)
  mat tc[1,`l']=B[1,2]
  mat tc[2,`l']=sqrt(S[2,2])
  mat tc[3,`l']=B[1,3]
  mat tc[4,`l']=sqrt(S[3,3])
  mat tc[1,3+`l']=B[1,148]
  mat tc[2,3+`l']=sqrt(S[148,148])
  mat tc[3,3+`l']=B[1,149]
  mat tc[4,3+`l']=sqrt(S[149,149])
  mat tc[1,6+`l']=B[1,293]
  mat tc[2,6+`l']=sqrt(S[293,293])
  mat tc[3,6+`l']=B[1,294]
  mat tc[4,6+`l']=sqrt(S[294,294])
  mat tc[1,9+`l']=B[1,425]
  mat tc[2,9+`l']=sqrt(S[425,425])
  mat tc[3,9+`l']=B[1,426]
  mat tc[4,9+`l']=sqrt(S[426,426])
  g e1sample=e(sample)
  
  ** Diagnostics **
  test ([gs]L`l'taxany1=[trade]L`l'taxany1)
  
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxany1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
  
  local m=0
  foreach y in gs trade corp income{
	qui xtreg `y' l.`y' L`l'taxany1 L`l'IMFnn $L1 _Iyear* if CGyears & e1sample, fe
	mat r2[1,3*`m'+`l']=e(N)
	mat r2[2,3*`m'+`l']=e(r2_w)
	local m=`m'+1
	}
  drop L`l'* L`k'* e1sample
  }

  ** Table 3 **
  estout e21 using "SCID_Table 2_L1.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) drop(_I* _cons) keep() order(*IMF* $L1) unstack 
  estout e22 using "SCID_Table 2_L2.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) drop(_I* _cons) keep() order(*IMF* $L1) unstack 
  estout e23 using "SCID_Table 2_L3.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) drop(_I* _cons) keep() order(*IMF* $L1) unstack 
  mat li tc
  mat li r2  
   
  ** p([gs]l.taxany1-[trade]l.taxany1)=0.0007 
  ** p([gs]l2.taxany1-[trade]l2.taxany1)=0.1271
  ** p([gs]l3.taxany1-[trade]l3.taxany1)=0.1972

  
* SUR restrictions
  
  mat sur=J(12,6,0)
  
  forvalues l=1/3{
    est restore e2`l'
    mat sur[4*`l'-3,1]=([gs]L`l'taxany1-[trade]L`l'taxany1)
    qui test ([gs]L`l'taxany1=[trade]L`l'taxany1)
    mat sur[4*`l'-2,1]=r(p)
	mat sur[4*`l'-1,1]=([gs]L`l'IMFnn-[trade]L`l'IMFnn)
    qui test ([gs]L`l'IMFnn=[trade]L`l'IMFnn)
    mat sur[4*`l',1]=r(p)
	
	mat sur[4*`l'-3,2]=([gs]L`l'taxany1-[corp]L`l'taxany1)
    qui test ([gs]L`l'taxany1=[corp]L`l'taxany1)
    mat sur[4*`l'-2,2]=r(p)
	mat sur[4*`l'-1,2]=([gs]L`l'IMFnn-[corp]L`l'IMFnn)
    qui test ([gs]L`l'IMFnn=[corp]L`l'IMFnn)
    mat sur[4*`l',2]=r(p)
	
	mat sur[4*`l'-3,3]=([gs]L`l'taxany1-[income]L`l'taxany1)
    qui test ([gs]L`l'taxany1=[income]L`l'taxany1)
    mat sur[4*`l'-2,3]=r(p)
	mat sur[4*`l'-1,3]=([gs]L`l'IMFnn-[income]L`l'IMFnn)
    qui test ([gs]L`l'IMFnn=[income]L`l'IMFnn)
    mat sur[4*`l',3]=r(p)
	
	mat sur[4*`l'-3,4]=([trade]L`l'taxany1-[corp]L`l'taxany1)
    qui test ([trade]L`l'taxany1=[corp]L`l'taxany1)
    mat sur[4*`l'-2,4]=r(p)
	mat sur[4*`l'-1,4]=([trade]L`l'IMFnn-[corp]L`l'IMFnn)
    qui test ([trade]L`l'IMFnn=[corp]L`l'IMFnn)
    mat sur[4*`l',4]=r(p)
	
	mat sur[4*`l'-3,5]=([trade]L`l'taxany1-[income]L`l'taxany1)
    qui test ([trade]L`l'taxany1=[income]L`l'taxany1)
    mat sur[4*`l'-2,5]=r(p)
	mat sur[4*`l'-1,5]=([trade]L`l'IMFnn-[income]L`l'IMFnn)
    qui test ([trade]L`l'IMFnn=[income]L`l'IMFnn)
    mat sur[4*`l',5]=r(p)
	
	mat sur[4*`l'-3,6]=([corp]L`l'taxany1-[income]L`l'taxany1)
    qui test ([corp]L`l'taxany1=[income]L`l'taxany1)
    mat sur[4*`l'-2,6]=r(p)
	mat sur[4*`l'-1,6]=([corp]L`l'IMFnn-[income]L`l'IMFnn)
    qui test ([corp]L`l'IMFnn=[income]L`l'IMFnn)
    mat sur[4*`l',6]=r(p)
  }

  ** Table B3 **
  mat li sur
   
   
*************************
* Specific tax conditions	  
*************************

  mat tc=J(4,12,0)
  mat r2=J(4,12,0)

  forvalues l=1/3{
  
  local k=`l'+1
  di "Lag `l'"
  
  foreach var in lngdppc totnontax trade_WDI va_agr_gdp_WDI gdp_growth_WDI{
    qui g L`l'`var'=l`l'.`var'
	}
  foreach var in IMFcum nUnder s_unga3g7 lngdppc gdp_growth reserves extbal debtser_gni fhindex dpi_exelec{
    qui g L`k'`var'=l`k'.`var'
	}
  foreach var in IMFnn taxany1 taxgst1 taxtr1 taxcrp1 imf_co imf_ro{
    qui g L`l'`var'=l`l'.`var'
	}
  
  global L1 L`l'lngdppc L`l'totnontax L`l'trade_WDI L`l'va_agr_gdp_WDI L`l'gdp_growth_WDI
  global ZL L`k'IMFcum L`k'nUnder L`k's_unga3g7 L`k'lngdppc L`k'gdp_growth L`k'reserves L`k'extbal L`k'debtser_gni L`k'fhindex L`k'dpi_exelec _Iregid*
  global TL L`l'IMFnn L`l'imf_co L`l'imf_ro
  
  ** Estimation: GST conditionality ** 
  di "Gst"
  local m=0  
  qui cmp (gs=l.gs L`l'taxgst1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxgst1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(200)
  est store egst`l'
  g e1sample=e(sample)
  
  ** Diagnostics **
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxgst1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
  
  qui xtreg gs l.gs L`l'taxgst1 L`l'IMFnn $L1 _Iyear* if CGyears & e1sample, fe
  mat r2[1,3*`m'+`l']=e(N)
  mat r2[2,3*`m'+`l']=e(r2_w)

  ** Estimation: TRT conditionality ** 
  di "Trd"
  local m=1
  qui cmp (trade=l.trade L`l'taxtr1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxtr1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(200)
  est store etr`l'
  drop e1sample
  g e1sample=e(sample)
  
  ** Diagnostics **
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxtr1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
  
  qui xtreg trade l.trade L`l'taxtr1 L`l'IMFnn $L1 _Iyear* if CGyears & e1sample, fe
  mat r2[1,3*`m'+`l']=e(N)
  mat r2[2,3*`m'+`l']=e(r2_w)

  ** Estimation: Corporate conditionality ** 
  di "corp"
  qui cmp (corp=l.corp L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxany1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(200) 
  est store ecrp`l'
  drop e1sample
  g e1sample=e(sample)
  
  ** Diagnostics **
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxany1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
  
  local m=2
  qui xtreg corp l.corp L`l'taxcrp1 L`l'IMFnn $L1 _Iyear* if CGyears & e1sample, fe
  mat r2[1,3*`m'+`l']=e(N)
  mat r2[2,3*`m'+`l']=e(r2_w)

  ** Estimation: Income conditionality ** 
  di "inc"
  qui cmp (income=l.income L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxany1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(200)
  est store einc`l'
  drop e1sample
  g e1sample=e(sample)
  
  ** Diagnostics **
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxany1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
  
  local m=3  
  qui xtreg income l.income L`l'taxany1 L`l'IMFnn $L1 _Iyear* if CGyears & e1sample, fe
  mat r2[1,3*`m'+`l']=e(N)
  mat r2[2,3*`m'+`l']=e(r2_w)

  
  drop L`l'* L`k'* e1sample
  }

  ** Table 4 **
  estout egst* using "SCID_Table 3_GST.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) keep(*tax* *IMF*) unstack 
  estout etr* using "SCID_Table 3_TRD.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) keep(*tax* *IMF*) unstack 
  estout ecrp* using "SCID_Table 3_CORP.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) keep(*tax* *IMF*) unstack 
  estout einc* using "SCID_Table 3_INC.rtf", starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) keep(*tax* *IMF*) unstack 

  mat li r2  
	  	  

		  
* Robustness tests
* ****************************************************************************  
   
   
* Alternative instrument

  mat tc=J(4,12,0)
  mat r2=J(4,12,0)

  forvalues l=1/3{
  
  local k=`l'+1
  di "Lag `l'"
  
  foreach var in lngdppc totnontax trade_WDI va_agr_gdp_WDI gdp_growth_WDI{
    qui g L`l'`var'=l`l'.`var'
	}
  foreach var in IMFcum nUnder s_unga3g7 lngdppc gdp_growth reserves extbal debtser_gni fhindex dpi_exelec{
    qui g L`k'`var'=l`k'.`var'
	}
  foreach var in IMFnn sp_taxany1 taxany1 taxgst1 taxtr1 taxcrp1 imf_co imf_ro{
    qui g L`l'`var'=l`l'.`var'
	}
  
  global L1 L`l'lngdppc L`l'totnontax L`l'trade_WDI L`l'va_agr_gdp_WDI L`l'gdp_growth_WDI
  global ZL L`k'IMFcum L`k'nUnder L`k's_unga3g7 L`k'lngdppc L`k'gdp_growth L`k'reserves L`k'extbal L`k'debtser_gni L`k'fhindex L`k'dpi_exelec _Iregid*
  global TL L`l'IMFnn L`l'sp_taxany1
  
  ** Estimation ** 
  qui cmp (gs=l.gs L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (trade=l.trade L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (corp=l.corp L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (income=l.income L`l'taxany1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxany1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(100) difficult
  est store ae4`l'
  mat B=e(b)
  mat S=e(V)
  mat tc[1,`l']=B[1,2]
  mat tc[2,`l']=sqrt(S[2,2])
  mat tc[3,`l']=B[1,3]
  mat tc[4,`l']=sqrt(S[3,3])
  mat tc[1,3+`l']=B[1,148]
  mat tc[2,3+`l']=sqrt(S[148,148])
  mat tc[3,3+`l']=B[1,149]
  mat tc[4,3+`l']=sqrt(S[149,149])
  mat tc[1,6+`l']=B[1,293]
  mat tc[2,6+`l']=sqrt(S[293,293])
  mat tc[3,6+`l']=B[1,294]
  mat tc[4,6+`l']=sqrt(S[294,294])
  mat tc[1,9+`l']=B[1,425]
  mat tc[2,9+`l']=sqrt(S[425,425])
  mat tc[3,9+`l']=B[1,426]
  mat tc[4,9+`l']=sqrt(S[426,426])
  g e1sample=e(sample)
  
  ** Diagnostics **
  test ([gs]L`l'taxany1=[trade]L`l'taxany1)
  
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxany1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
  
  local m=0
  foreach y in gs trade corp income{
	qui xtreg `y' l.`y' L`l'taxany1 L`l'IMFnn $L1 _Iyear* if e1sample, fe
	mat r2[1,3*`m'+`l']=e(N)
	mat r2[2,3*`m'+`l']=e(r2_w)
	local m=`m'+1
	}
  drop L`l'* L`k'* e1sample
  }

  ** Table C1 **
  mat li tc
  mat li r2  
   
  ** p([gs]l.taxany1-[trade]l.taxany1)=0.0007
  ** p([gs]l2.taxany1-[trade]l2.taxany1)=0.
  ** p([gs]l3.taxany1-[trade]l3.taxany1)=0.799
   

* GMM approach 

  mat Beta1=J(18,5,0)
  mat Diag1=J(15,5,0)
  local m=1

  foreach y in tottax gs trade income corp{
  di "`y'"
  g y=`y'
  local t=0
  
  foreach tax in taxgst1 taxtr1 taxcrp1{

  g IMFw=(IMFnn==1 & `tax'>0)
  g IMFwo=(IMFnn==1 & `tax'==0)

  qui xtabond2 y L.y l.IMFw l.IMFwo $CGL1 _Iyear* if CGyears, gmm(IMFcum nUnder s_unga3g7 reserves lngdppc gdp_growth extbal debtser_gni fhindex dpi_exelec _Iregid*, lag(2 2)) robust 
  mat B=e(b)
  mat S=e(V)
  mat Beta1[1+6*`t',`m']=B[1,2]
  mat Beta1[3+6*`t',`m']=B[1,3]
  mat Beta1[5+6*`t',`m']=B[1,1]
  mat Beta1[2+6*`t',`m']=sqrt(S[2,2])
  mat Beta1[4+6*`t',`m']=sqrt(S[3,3])
  mat Beta1[6+6*`t',`m']=sqrt(S[1,1])
  mat Diag1[1+5*`t',`m']=e(N)
  mat Diag1[2+5*`t',`m']=e(j)
  mat Diag1[3+5*`t',`m']=e(sarganp)
  mat Diag1[4+5*`t',`m']=e(hansenp)
  mat Diag1[5+5*`t',`m']=e(ar2p)
  local t=`t'+1
  drop IMFw IMFwo
  }
  local m=`m'+1
  drop y 
  }
  
  ** Table C2 ** 
  mat li Beta1
  mat li Diag1


* IMF program (1980-2013)
  
  mat r2=J(4,12,0)

  forvalues l=1/3{
  
  local k=`l'+1
  qui g LagIMFnn=l`l'.IMFnn
  di "Lag `l'"
  
  foreach var in lngdppc totnontax trade_WDI va_agr_gdp_WDI gdp_growth_WDI{
    qui g Lag`var'=l`l'.`var'
	}
  foreach var in IMFcum nUnder s_unga3g7 lngdppc gdp_growth reserves extbal debtser_gni fhindex dpi_exelec{
    qui g Lagz`var'=l`k'.`var'
	}
  
  global L1 Laglngdppc Lagtotnontax Lagtrade_WDI Lagva_agr_gdp_WDI Laggdp_growth_WDI
  global ZL LagzIMFcum LagznUnder Lagzs_unga3g7 Lagzlngdppc Lagzgdp_growth Lagzreserves Lagzextbal Lagzdebtser_gni Lagzfhindex Lagzdpi_exelec _Iregid*
  
  ** Estimation **
  qui cmp (gs=l.gs LagIMFnn $L1 _Icid* _Iyear*) (trade=l.trade LagIMFnn $L1 _Icid* _Iyear*) (corp=l.corp LagIMFnn $L1 _Icid* _Iyear*) (income=l.income LagIMFnn $L1 _Icid* _Iyear*) (LagIMFnn=$ZL _Iyear*), indicators($cmp_cont $cmp_cont $cmp_cont $cmp_cont $cmp_probit) ro cl(cid) iterate(200)
  est store ae1`l'
  g e1sample=e(sample)
  
  ** Diagnostics **
  qui probit LagIMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  local m=0
  foreach y in gs trade corp income{
	qui xtreg `y' l.`y' LagIMFnn $L1 _Iyear* if e1sample, fe
	mat r2[1,3*`m'+`l']=e(N)
	mat r2[2,3*`m'+`l']=e(r2_w)
	local m=`m'+1
	}
  drop Lag* e1sample
  }

  ** Table C3 **
  estout ae1*, starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 N2, fmt(%7.0f %7.0f)) drop(_I* _cons) keep() order(*IMF* $L1) unstack 
  mat li r2    
  

* Tax conditionality (1980-2013)

  mat ra3=J(4,12,0)
  mat r2=J(4,12,0)

  forvalues l=1/3{
  
  local k=`l'+1
  di "Lag `l'"
  
  foreach var in lngdppc totnontax trade_WDI va_agr_gdp_WDI gdp_growth_WDI{
    qui g L`l'`var'=l`l'.`var'
	}
  foreach var in IMFcum nUnder s_unga3g7 lngdppc gdp_growth reserves extbal debtser_gni fhindex dpi_exelec{
    qui g L`k'`var'=l`k'.`var'
	}
  foreach var in IMFnn taxany1 taxgst1 taxtr1 taxcrp1 imf_co imf_ro{
    qui g L`l'`var'=l`l'.`var'
	}
  
  global L1 L`l'lngdppc L`l'totnontax L`l'trade_WDI L`l'va_agr_gdp_WDI L`l'gdp_growth_WDI
  global ZL L`k'IMFcum L`k'nUnder L`k's_unga3g7 L`k'lngdppc L`k'gdp_growth L`k'reserves L`k'extbal L`k'debtser_gni L`k'fhindex L`k'dpi_exelec _Iregid*
  global TL L`l'IMFnn L`l'imf_co L`l'imf_ro
  
  ** Estimation: GST conditionality ** 
  di "Gst"
  local m=0
  qui cmp (gs=l.gs L`l'taxgst1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxgst1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(200)
  est store aegst`l'
    mat B=e(b)
	mat S=e(V)
	mat ra3[1,3*`m'+`l']=B[1,2]
	mat ra3[3,3*`m'+`l']=B[1,3]
	mat ra3[2,3*`m'+`l']=sqrt(S[2,2])
	mat ra3[4,3*`m'+`l']=sqrt(S[2,2])
  g e1sample=e(sample)
  
  ** Diagnostics **
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxgst1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
    
  qui xtreg gs l.gs L`l'taxgst1 L`l'IMFnn $L1 _Iyear* if CGyears & e1sample, fe
  mat r2[1,3*`m'+`l']=e(N)
  mat r2[2,3*`m'+`l']=e(r2_w)

  ** Estimation: TRT conditionality ** 
  di "Trd"
  local m=1
  qui cmp (trade=l.trade L`l'taxtr1 L`l'IMFnn $L1 _Icid* _Iyear*) (L`l'IMFnn=$ZL _Iyear*) (L`l'taxtr1=$TL $L1 _Iyear*) if CGyears, indicators($cmp_cont $cmp_probit $cmp_probit) ro cl(cid) iterate(200)
  est store aetr`l'
    mat B=e(b)
	mat S=e(V)
    mat ra3[1,3*`m'+`l']=B[1,2]
	mat ra3[3,3*`m'+`l']=B[1,3]
	mat ra3[2,3*`m'+`l']=sqrt(S[2,2])
	mat ra3[4,3*`m'+`l']=sqrt(S[2,2])
  drop e1sample
  g e1sample=e(sample)
  
  ** Diagnostics **
  qui probit L`l'IMFnn $ZL _Iyear* if e1sample
  mat r2[3,`l']=e(r2_p)
  mat r2[3,3+`l']=e(r2_p)
  mat r2[3,6+`l']=e(r2_p)
  mat r2[3,9+`l']=e(r2_p)
  
  qui probit L`l'taxtr1 $TL $L1 _Iyear* if e1sample 
  mat r2[4,`l']=e(r2_p)
  mat r2[4,3+`l']=e(r2_p)
  mat r2[4,6+`l']=e(r2_p)
  mat r2[4,9+`l']=e(r2_p)
  
  qui xtreg trade l.trade L`l'taxtr1 L`l'IMFnn $L1 _Iyear* if CGyears & e1sample, fe
  mat r2[1,3*`m'+`l']=e(N)
  mat r2[2,3*`m'+`l']=e(r2_w)
  
  drop L`l'* L`k'* e1sample
  }

  ** Table C4 **
  mat li ra3
  mat li r2
   
   
   
   
   
* Appendix D
************   

* Tax adoption

  mat R2fit=J(2,12,0)
  mat A=J(4,12,0)
  local m=0
  
  foreach x in taxgst1 taxtr1 taxcrp1{
  
    local m=`m'+1
	di "condition `x'"
	
	forvalues l=1/4{
	
	local k=`l'+1
	di "Lag `l'"
	global L1 l`l'.lngdppc l`l'.totnontax l`l'.trade_WDI l`l'.va_agr_gdp_WDI l`l'.gdp_growth_WDI
    global ZL l`k'.IMFcum l`k'.nUnder l`k'.s_unga3g7 l`k'.lngdppc l`k'.gdp_growth l`k'.reserves l`k'.extbal l`k'.debtser_gni l`k'.fhindex l`k'.dpi_exelec _Iregid*
	g durdep=max(0, year-1980-`l')
    replace durdep=ln(1+durdep)
  
	qui cmp (VAT1=l`l'.`x' l`l'.IMFnn $L1 durdep _Iregid*) (l`l'.IMFnn=$ZL _Iyear*) if CGyears, indicators($cmp_probit $cmp_probit) cl(cid) iterate(100) difficult
    est store T3B`m'`l'
	mat B=e(b)
	mat S=e(V)
	mat A[1,4*(`m'-1)+`l']=B[1,1]
	mat A[2,4*(`m'-1)+`l']=sqrt(S[1,1])
	mat A[3,4*(`m'-1)+`l']=B[1,2]
	mat A[4,4*(`m'-1)+`l']=sqrt(S[2,2])
      qui probit l`l'.IMFnn $ZL _Iyear* if CGyears
	mat R2fit[1,4*(`m'-1)+`l']=e(N)
	mat R2fit[2,4*(`m'-1)+`l']=e(r2_p)
	drop durdep
	}
  }
  
  estout T3B*, starlevels(* .1 ** .05 *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N r2_p, fmt(%7.0f %7.2f)) drop(_I*) keep(VAT1:) order(*IMF* *tax*)  
  mat li A
  mat li R2fit 
	

***
